package com.glory.erp.invoice.bill.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.glory.erp.invoice.bill.bean.BillQuery;
import com.glory.erp.invoice.bill.dao.BillDao;
import com.glory.erp.invoice.orderdetail.bean.OrderDetail;

public class BillDaoImpl extends HibernateDaoSupport implements BillDao{

	public List<Object[]> findBuyBill(BillQuery billQuery) {
		// String hql = "select goods,sum(num) from OrderDetail group by goods.uuid";
		DetachedCriteria dc = DetachedCriteria.forClass(OrderDetail.class);
		//设置一条查询结果内容为多个内容
		ProjectionList plist = Projections.projectionList();
		// 分组(HQL,SQL中使用group by完成，QBC中使用投影
		plist.add(Projections.groupProperty("goods"));
		// select 内容
		plist.add(Projections.sum("num"));
		dc.setProjection(plist);
		
		// 条件
		dc.createAlias("order", "o");
		if(billQuery.getType()!=null && billQuery.getType() != -1){
			dc.add(Restrictions.eq("o.type", billQuery.getType()));
		}
		if(billQuery.getSupplierUuid()!=null && billQuery.getSupplierUuid() != -1){
			dc.createAlias("o.supplier", "s");
			dc.add(Restrictions.eq("s.uuid", billQuery.getSupplierUuid()));
		}
		return this.getHibernateTemplate().findByCriteria(dc);
	}
}